﻿#region License
/* ---------------------------------------------------------------- *
 *
 * This file is part of the API Concepts Framework
 * http://apiconceptsframework.codeplex.com
 *
 * Copyright (C) 2014 eva Kühn, Thomas Scheller, Vienna University of Technology
 * http://www.complang.tuwien.ac.at/eva
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/
 *
 * ---------------------------------------------------------------- */
#endregion

using System.Reflection;
using ApiConceptsFramework.Contract;
using System;
using System.Collections.Generic;
using System.Linq;

namespace ApiConceptsFramework.HCs
{
	public class ConfigString : IHighLevelConcept
	{
		public const float ConfigStringElementSearchComplexity = 9; //same as XML attribute without schema
		public const float ConfigStringElementUsageComplexity = 9; //same as XML attribute without schema
		public static readonly char[] PossibleConfigStringSeparators = new[]{';',','};

		private static readonly LearningEffect UsageLearningEffect = new LearningEffect(0.5f, 1.5f); //same learning effect as XML

		public Type InputDataType { get { return typeof (ConfigStringInputData); } }

		public HcEvaluationResult Evaluate(List<Assembly> apiAssemblies, IEnumerable<ConceptInstance> data, IHcEvaluationInput hcInputData, IEnumerable<HcEvaluationResult> otherHcData, IDE ide = IDE.VisualStudioOrSimilar)
		{
			if (hcInputData == null)
				return null;
			var pd = hcInputData as ConfigStringInputData;
			if (pd == null)
				throw new ArgumentException("hcInputData must be ConfigStringInputData", "hcInputData");
			if (pd.ConfigStrings.Count == 0)
				return null;

			return new HcEvaluationResult
				{
					HighLevelConcept = this,
					GeneralComplexity = 0,
					ComplexityData = pd.ConfigStrings.Select(CalculateComplexityOfConfigString).ToList()
				};
		}

		private HcComplexityData CalculateComplexityOfConfigString(ConfigStringValue configStringValue)
		{
			//count all elements with length > 0
			var numberOfElements = configStringValue.ConfigString.Split(PossibleConfigStringSeparators).Count(s => s.Length > 0);
			var searchComplexity = numberOfElements * ConfigStringElementSearchComplexity;
			var usageComplexity = numberOfElements * ConfigStringElementUsageComplexity;

			return new HcComplexityData
				{
					HighLevelConcept = this,
					SearchComplexity = LearningEffect.Search.CalculateComplexityForMultipleUsages(searchComplexity, configStringValue.NumberOfUsages, configStringValue.PreviousUsages),
					UsageComplexity = UsageLearningEffect.CalculateComplexityForMultipleUsages(usageComplexity, configStringValue.NumberOfUsages, configStringValue.PreviousUsages),
					Description = "Config string: " + configStringValue.ConfigString + " (" + configStringValue.NumberOfUsages + " usages)",
					NumberOfUsages = configStringValue.NumberOfUsages,
				};
		}
	}
}
